package com.mmc.executor;

import com.mmc.container.Configration;
import com.mmc.container.MappedStatement;
import com.mmc.executor.sqldeal.BoundSql;
import com.mmc.executor.sqldeal.ParameterMapping;
import com.mmc.executor.sqldeal.ParameterMappingTokenHandler;
import com.mmc.utils.GenericTokenParser;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class SimpleExecutor implements Executor {

    @Override
    public <T> List<T> queryList(Configration configration,MappedStatement mappedStatement, Object... params) throws SQLException, NoSuchFieldException, IllegalAccessException, IntrospectionException, InstantiationException, InvocationTargetException {
        String statementSql = mappedStatement.getSql();
        BoundSql boundSql = getBoundSql(statementSql);
        Class paramterType = mappedStatement.getParamterType();
        //获取连接
        Connection connection = configration.getDataSource().getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(boundSql.getSql());
        List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
        for (int i = 0; i < parameterMappings.size(); i++) {
            ParameterMapping parameterMapping = parameterMappings.get(i);
            String content = parameterMapping.getContent();
            Field declaredField = paramterType.getDeclaredField(content);
            declaredField.setAccessible(true);
            Object o = declaredField.get(params[0]);
            preparedStatement.setObject(i+1,o);
        }

        ResultSet resultSet = preparedStatement.executeQuery();
        List<T> results = new ArrayList<T>();
        Class resultType = mappedStatement.getResultType();
        while (resultSet.next()) {
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();
            Object o = resultType.newInstance();
            for (int i = 1; i <= columnCount; i++) {
                //属性名
                String columnName = metaData.getColumnName(i);
                //属性值
                Object value = resultSet.getObject(columnName);
                //创建属性描述器，为属性⽣成读写⽅法
                PropertyDescriptor propertyDescriptor = new
                        PropertyDescriptor(columnName, resultType);
                //获取写⽅法
                Method writeMethod = propertyDescriptor.getWriteMethod();
                //向类中写⼊值
                writeMethod.invoke(o, value);
            }
            results.add((T) o);
        }
        return results;

    }

    private BoundSql getBoundSql(String statementSql) {
        ParameterMappingTokenHandler parameterMappingTokenHandler=new ParameterMappingTokenHandler();
        GenericTokenParser genericTokenParser=new GenericTokenParser("#{","}",parameterMappingTokenHandler);
        String parseSql = genericTokenParser.parse(statementSql);
        return new BoundSql(parseSql,parameterMappingTokenHandler.getParameterMappings());
    }
}
